package com.shiyanlou.upms.system.controller;

import com.shiyanlou.upms.common.domain.Tree;
import com.shiyanlou.upms.common.utils.R;
import com.shiyanlou.upms.system.domain.UpmsPermission;
import com.shiyanlou.upms.system.domain.UpmsSystem;
import com.shiyanlou.upms.system.service.UpmsPermissionService;
import com.shiyanlou.upms.system.service.UpmsSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 资源管理
 */
@Controller
@RequestMapping("/permission")
public class UpmsPermissionController {

    @Autowired
    private UpmsPermissionService upmsPermissionService;

    @Autowired
    private UpmsSystemService upmsSystemService;

    @GetMapping("/main")
    String UpmsPermission(){
        return "permission/main";
    }

    @ResponseBody
    @GetMapping("/list")
    public List<UpmsPermission> list(@RequestParam Map<String, Object> params){
        List<UpmsPermission> upmsPermissionList = upmsPermissionService.list(params);
        return upmsPermissionList;
    }

    @GetMapping("/add/{pid}")
    String add(@PathVariable("pid") Integer pid, Model model){
        List<UpmsSystem> systemList = upmsSystemService.list(new HashMap<>());
        UpmsPermission pPermission = upmsPermissionService.selectByPrimaryKey(pid);
        if(pPermission == null || pPermission.getPermissionId() == null) {
            pPermission = new UpmsPermission();
            pPermission.setPermissionId(0);
            pPermission.setName("根目录");
        }
        model.addAttribute("systemList", systemList);
        model.addAttribute("pPermissionDO", pPermission);
        return "permission/add";
    }

    @GetMapping("/edit/{permissionId}")
    String edit(@PathVariable("permissionId") Integer permissionId,Model model){
        UpmsPermission upmsPermission = upmsPermissionService.selectByPrimaryKey(permissionId);
        List<UpmsSystem> systemList = upmsSystemService.list(new HashMap<>());
        UpmsPermission pPermissionDO = upmsPermissionService.selectByPrimaryKey(upmsPermission.getPid());
        if(pPermissionDO == null || pPermissionDO.getPermissionId() == null) {
            pPermissionDO = new UpmsPermission();
            pPermissionDO.setPermissionId(0);
            pPermissionDO.setName("根目录");
        }
        model.addAttribute("upmsPermission", upmsPermission);
        model.addAttribute("pPermissionDO", pPermissionDO);
        model.addAttribute("systemList", systemList);
        return "permission/edit";
    }

    @ResponseBody
    @PostMapping("/save")
    public R save(UpmsPermission upmsPermission){
        if(upmsPermissionService.insert(upmsPermission)>0){
            return R.ok();
        }
        return R.error();
    }


    @ResponseBody
    @RequestMapping("/update")
    public R update( UpmsPermission upmsPermission){
        upmsPermissionService.updateByPrimaryKeySelective(upmsPermission);
        return R.ok();
    }

    @PostMapping( "/remove")
    @ResponseBody
    public R remove( Integer permissionId){
        if(upmsPermissionService.deleteByPrimaryKey(permissionId)>0){
            return R.ok();
        }
        return R.error();
    }


    @PostMapping("/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] permissionIds) {
        upmsPermissionService.batchRemove(permissionIds);
        return R.ok();
    }

    @GetMapping("/getPermissionTree/{roleId}")
    public String getPermissionTree(@PathVariable("roleId") Integer roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return "permission/tree";
    }

    /**
     * 获取一个角色的资源树
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/getTree")
    public List<Tree<UpmsPermission>> getTree(Integer roleId) {
        List<Tree<UpmsPermission>> treeList = new ArrayList<Tree<UpmsPermission>>();
        treeList = upmsPermissionService.getTree(roleId);
        return treeList;
    }

}
